package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.entity.SpuAttrValueEntity;
import com.atguigu.gmall.pms.mapper.AttrMapper;
import com.atguigu.gmall.pms.mapper.SkuAttrValueMapper;
import com.atguigu.gmall.pms.mapper.SpuAttrValueMapper;
import com.atguigu.gmall.pms.vo.AttrValueVo;
import com.atguigu.gmall.pms.vo.ItemGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.AttrGroupMapper;
import com.atguigu.gmall.pms.entity.AttrGroupEntity;
import com.atguigu.gmall.pms.service.AttrGroupService;
import org.springframework.util.CollectionUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrMapper attrMapper;

    @Autowired
    AttrGroupMapper attrGroupMapper;

    @Autowired
    SpuAttrValueMapper spuAttrValueMapper;

    @Autowired
    SkuAttrValueMapper skuAttrValueMapper;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<AttrGroupEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<AttrGroupEntity> queryGroupsWithAttrsByCid(Long cid) {

        // 1.根据分类id查询分组
        List<AttrGroupEntity> groupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("category_id", cid));

        if (CollectionUtils.isEmpty(groupEntities)){
            return null;
        }

        // 2.遍历所有的分组，根据分组的id查询分组下的规格参数
        groupEntities.forEach(group -> {
            List<AttrEntity> attrEntities = this.attrMapper.selectList(new QueryWrapper<AttrEntity>().eq("group_id", group.getId()).eq("type", 1));
            group.setAttrEntities(attrEntities);
        });

        return groupEntities;
    }

    /**
     *规格参数组及组下的规格参数(带值)
     *pms_sku里的category_id查pms_attr_group里的id,name,sort,icon
     * SELECT * FROM pms_attr_group WHERE category_id=225;
     *再通过category_id和group_id查询pms_attr下的参数
     * SELECT * FROM pms_attr WHERE category_id=225 AND group_id IN (1,2,3);
     *再根据pms_attr下的type值的不同,type=1根据spu_id查询pms_spu_attr_value获得具体值,type=0根据sku_id查询pms_sku_attr_value获得具体值
     * SELECT * FROM pms_spu_attr_value WHERE spu_id=7 AND attr_id IN (1,2,6,7);
     *
     * SELECT * FROM pms_sku_attr_value WHERE sku_id=28 AND attr_id IN (3);
     */
    @Override
    public List<ItemGroupVo> queryGroupWithAttrValuesByCidAndSpuIdAndSkuId(Long cid,Long spuId,Long skuId) {
        List<AttrGroupEntity> groupEntities = attrGroupMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq(
                "category_id", cid));
        if(CollectionUtils.isEmpty(groupEntities)){
            return null;
        }
        //目的将List<AttrGroupEntity>改为 List<ItemGroupVo>
     return groupEntities.stream().map(group->{
           ItemGroupVo itemGroupVo = new ItemGroupVo();
           itemGroupVo.setId(group.getId());
           itemGroupVo.setName(group.getName());
           // 遍历分组，查询每个分组下的规格参数
           List<AttrEntity> attrEntities = attrMapper.selectList(new QueryWrapper<AttrEntity>().eq("category_id",
                   cid));
           if(CollectionUtils.isEmpty(attrEntities)){
               return itemGroupVo;
           }


           List<Long> attrIds = attrEntities.stream().map(AttrEntity::getId).collect(Collectors.toList());

           List<AttrValueVo> attrValueVos = new ArrayList<>();
           // 查询基本属性及值
           List<SpuAttrValueEntity> baseAttrValues =
                   spuAttrValueMapper.selectList(new QueryWrapper<SpuAttrValueEntity>().eq("spu_id", spuId).in(
                           "attr_id", attrIds));

           if(!CollectionUtils.isEmpty(baseAttrValues)){
                attrValueVos.addAll(baseAttrValues.stream().map(SpuAttrValueEntity->{
                    AttrValueVo attrValueVo = new AttrValueVo();
                    BeanUtils.copyProperties(SpuAttrValueEntity,attrValueVo);
                    return attrValueVo;
                }).collect(Collectors.toList()));
           }
           // 查询销售属性及值
           List<SkuAttrValueEntity> saleAttrValues =
                   skuAttrValueMapper.selectList(new QueryWrapper<SkuAttrValueEntity>().eq("sku_id", skuId).in(
                           "attr_id", attrIds));

           if(!CollectionUtils.isEmpty(saleAttrValues)){
               attrValueVos.addAll(saleAttrValues.stream().map(SkuAttrValueEntity->{
                   AttrValueVo attrValueVo = new AttrValueVo();
                   BeanUtils.copyProperties(SkuAttrValueEntity,attrValueVo);
                   return attrValueVo;
               }).collect(Collectors.toList()));
           }

           itemGroupVo.setAttrValues(attrValueVos);
           return itemGroupVo;
       }).collect(Collectors.toList());

    }

}
